package com.chuang.server.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chuang.server.mapper.TreeCategoryMapper;
import com.chuang.server.pojo.ResultBean;
import com.chuang.server.pojo.TreeCategory;
import com.chuang.server.pojo.User;
import com.chuang.server.service.TreeCategoryService;
import com.chuang.server.util.Token2User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 树形分类表
 服务实现类
 * </p>
 *
 * @author vicente
 * @since 2021-12-18
 */
@Service
public class TreeCategoryServiceImpl extends ServiceImpl<TreeCategoryMapper, TreeCategory> implements TreeCategoryService {
    @Autowired
    private TreeCategoryMapper treeCategoryMapper;
    @Autowired
    private Token2User token2User;
    @Override
    public ResultBean selectAll(){
        List<Map> list = treeCategoryMapper.list();
        return ResultBean.success("成功",list);
    }

    @Override
    public ResultBean byUserList(Long id) {
        List<Map> list = treeCategoryMapper.byUserList(id);
        return ResultBean.success("成功",list);
    }

    @Override
    public ResultBean addCategory(JSONObject jsonObject) {
        User user = token2User.user();
        jsonObject.put("userId",user.getId());
        jsonObject.forEach((key, value) -> {
            System.out.println(key + ":" + value);
        });
        int i = treeCategoryMapper.addCategory(jsonObject);
        return ResultBean.success("成功",i);
    }

    @Override
    public ResultBean delCategory(JSONObject jsonObject) {
        List ids = jsonObject.getJSONArray("ids");
        int i = treeCategoryMapper.delCategory(ids);
        return ResultBean.success("成功",i);
    }

    @Override
    public ResultBean updateCategory(JSONObject jsonObject) {
        int i = treeCategoryMapper.updateCategory(jsonObject);
        return ResultBean.success("成功",i);
    }
}
